package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 2025版驱动电机数据格式 (0x02)
 * 参考GB/T 32960.3-2025标准表15-16
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class DriveMotorData {
    // 驱动电机个数:1~253个驱动电机个数(由driveMotorList的totalUnit获取)
    private int driveMotorCount;
    
    @Field(totalUnit = 1, desc = "驱动电机总成信息列表")
    private List<DriveMotorInfo> driveMotorList;
    
    @ToString
    @Data
    @Accessors(chain = true)
    public static class DriveMotorInfo {
        @Field(length = 1, desc = "驱动电机序号:有效值范围1~253")
        private int driveMotorSequence;
        
        @Field(length = 1, desc = "驱动电机状态:0x01:耗电;0x02:发电;0x03:关闭状态;0x04:准备状态")
        private int driveMotorStatus;
        
        @Field(length = 1, desc = "驱动电机控制器温度:有效值范围0~250(偏移量40℃)")
        private int controllerTemperature;
        
        @Field(length = 2, desc = "驱动电机转速:有效值范围0~65531(偏移量32000r/min)")
        private int driveMotorSpeed;
        
        @Field(length = 4, desc = "驱动电机转矩:有效值范围0~400000(偏移量20000N·m,精度0.1N·m)")
        private int driveMotorTorque;
        
        @Field(length = 1, desc = "驱动电机温度:有效值范围0~250(偏移量40℃)")
        private int driveMotorTemperature;
        
        // ==================== 传输值与真实值转换方法 ====================
        
        /**
         * 获取驱动电机控制器温度 (℃)
         * @return 温度，精度1℃，异常返回-999，无效返回-1000
         */
        public int getControllerTemperature() {
            if (controllerTemperature == 0xFE) return -999; // 异常
            if (controllerTemperature == 0xFF) return -1000; // 无效
            return controllerTemperature - 40; // (传输值-40) = 真实值(℃)，支持负值
        }
        
        /**
         * 设置驱动电机控制器温度 (℃)
         * @param controllerTemperature 温度值，范围-40℃~+210℃
         */
        public void setControllerTemperature(int controllerTemperature) {
            if (controllerTemperature < -40 || controllerTemperature > 210) {
                this.controllerTemperature = 0xFF; // 无效值
            } else {
                this.controllerTemperature = controllerTemperature + 40; // (真实值+40) = 传输值
            }
        }
        
        /**
         * 获取驱动电机转速 (r/min)
         * @return 转速，异常返回-99999，无效返回-100000
         */
        public int getDriveMotorSpeed() {
            if (driveMotorSpeed == 0xFFFE) return -99999; // 异常
            if (driveMotorSpeed == 0xFFFF) return -100000; // 无效
            return driveMotorSpeed - 32000; // (传输值-32000) = 真实值(r/min)，支持负值
        }
        
        /**
         * 设置驱动电机转速 (r/min)
         * @param driveMotorSpeed 转速值，范围-32000~+33531 r/min
         */
        public void setDriveMotorSpeed(int driveMotorSpeed) {
            if (driveMotorSpeed < -32000 || driveMotorSpeed > 33531) {
                this.driveMotorSpeed = 0xFFFF; // 无效值
            } else {
                this.driveMotorSpeed = driveMotorSpeed + 32000; // (真实值+32000) = 传输值
            }
        }
        
        /**
         * 获取驱动电机转矩 (N·m)
         * @return 转矩，精度0.1N·m，异常返回-99999，无效返回-100000
         */
        public double getDriveMotorTorque() {
            if (driveMotorTorque == 0xFFFFFFFE) return -99999.0; // 异常
            if (driveMotorTorque == 0xFFFFFFFF) return -100000.0; // 无效
            return (driveMotorTorque - 200000) * 0.1; // (传输值-200000)×0.1 = 真实值(N·m)，支持负值
        }
        
        /**
         * 设置驱动电机转矩 (N·m)
         * @param driveMotorTorque 转矩值，范围-20000.0~+20000.0 N·m
         */
        public void setDriveMotorTorque(double driveMotorTorque) {
            if (driveMotorTorque < -20000.0 || driveMotorTorque > 20000.0) {
                this.driveMotorTorque = 0xFFFFFFFF; // 无效值
            } else {
                this.driveMotorTorque = (int) Math.round(driveMotorTorque * 10 + 200000); // (真实值×10)+200000 = 传输值
            }
        }
        
        /**
         * 获取驱动电机温度 (℃)
         * @return 温度，精度1℃，异常返回-999，无效返回-1000
         */
        public int getDriveMotorTemperature() {
            if (driveMotorTemperature == 0xFE) return -999; // 异常
            if (driveMotorTemperature == 0xFF) return -1000; // 无效
            return driveMotorTemperature - 40; // (传输值-40) = 真实值(℃)，支持负值
        }
        
        /**
         * 设置驱动电机温度 (℃)
         * @param driveMotorTemperature 温度值，范围-40℃~+210℃
         */
        public void setDriveMotorTemperature(int driveMotorTemperature) {
            if (driveMotorTemperature < -40 || driveMotorTemperature > 210) {
                this.driveMotorTemperature = 0xFF; // 无效值
            } else {
                this.driveMotorTemperature = driveMotorTemperature + 40; // (真实值+40) = 传输值
            }
        }
        
        /**
         * 检查驱动电机控制器温度是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isControllerTemperatureValid() {
            return controllerTemperature != 0xFE && controllerTemperature != 0xFF;
        }
        
        /**
         * 检查驱动电机转速是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isDriveMotorSpeedValid() {
            return driveMotorSpeed != 0xFFFE && driveMotorSpeed != 0xFFFF;
        }
        
        /**
         * 检查驱动电机转矩是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isDriveMotorTorqueValid() {
            return driveMotorTorque != 0xFFFFFFFE && driveMotorTorque != 0xFFFFFFFF;
        }
        
        /**
         * 检查驱动电机温度是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isDriveMotorTemperatureValid() {
            return driveMotorTemperature != 0xFE && driveMotorTemperature != 0xFF;
        }
    }
    
    // 通过driveMotorList列表长度返回driveMotorCount
    public int getDriveMotorCount() {
        return driveMotorList == null ? 0 : driveMotorList.size();
    }
}